//
//  MTTabBarController.swift
//  ThousandsEuropean
//
//  Created by apple on 2019/12/30.
//  Copyright © 2019 肖恩. All rights reserved.
//

import UIKit

class MTTabBarController: UITabBarController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        
        NotificationCenter.default.addObserver(self, selector: #selector(logNotifitionClicks), name: Notification.Name("LogOutNotification"), object: nil)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(true)
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        createViewControllers()
    }
    
    @objc func logNotifitionClicks(notifi : Notification) {
        UserDefaults.standard.removeObject(forKey: "token")
        UserDefaults.standard.removeObject(forKey: "SecondsKill")
        UserDefaults.standard.removeObject(forKey: "password")
        
        UserDefaults.standard.setValue(false, forKey: "isLogin")
        YProgressHUD.showToast("退出登录成功")
        let tab = HomeVC()
        let navVc = MTNavController(rootViewController: tab)
        UIApplication.shared.keyWindow?.rootViewController = navVc
   }
    
    func createViewControllers() {
        let home = HomeVC()
        let classif = WebLoadingViewController()
        classif.filePath = "http://swoole.whichzhen.com/api/user/app_h5?token=\(UserManager.shared.currentUser?.token ?? "")&uid=\(UserManager.shared.currentUser?.id ?? "")&type=1"
        let seconds = NewsVC()
        let personal = MeVC()
        
        let vcs = [home,classif,seconds,personal]
        
        let imges = ["首页","商城","资讯","我的"]
        let selectedImages = ["首页-选中","商城-选中","资讯-选中","我的-选中"]
        let titels = ["首页","商城","资讯","我的"]
        var navVCs : [UIViewController] = []
        
        for (index, vc) in vcs.enumerated() {
            // 未选中状态Tab图片
            vc.tabBarItem.image = UIImage(named: imges[index])?.withRenderingMode(.alwaysOriginal)
            // 选中状态Tab图片
            vc.tabBarItem.selectedImage = UIImage(named: selectedImages[index])?.withRenderingMode(.alwaysOriginal)
         
            vc.title = titels[index]
            
            let navVc = MTNavController(rootViewController: vc)
            navVCs.append(navVc)
            
        }
//        self.tabBar.tintColor = UIColor.init(r: 82, g: 75, b: 153)
//        UITabBar.appearance().isTranslucent = false
        self.tabBar.barTintColor = UIColor.white
        self.tabBar.backgroundImage = self.drawTabBarItemBackgroundImageWithSize(size: self.tabBar.bounds.size)
        
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font:UIFont.systemFont(ofSize: 12)], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:Gray_COLOR], for: UIControl.State.normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:Orange_COLOR], for: UIControl.State.selected)
        
        self.viewControllers = navVCs
    }
    
    func drawTabBarItemBackgroundImageWithSize(size:CGSize) -> (UIImage) {
        // 准备绘图环境
        UIGraphicsBeginImageContext(size);
        let ctx = UIGraphicsGetCurrentContext();

        ctx!.setFillColor(red: 255 / 255, green: 255 / 255, blue: 255 / 255, alpha: 1);
        ctx!.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height));

        // 获取该绘图中的图片
        let img = UIGraphicsGetImageFromCurrentImageContext();

        //结束绘图
        UIGraphicsEndImageContext();
        return img!;
    }
}
